/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sge.baseclassmanager;

import java.sql.*;
import sge.baseclass.CPF;
import sge.baseclass.Responsavel;

/**
 *
 * @author dolalima
 */
public abstract class GerenciadorDeResponsavel extends GerenciadorDeClasses {

        
    public static boolean cadastrar(Responsavel responsavel) {
        
        try {
            // Configuração de pre-comando
            preparedStatement = connection.prepareStatement("INSERT INTO responsavel(nome,"
                    + "sexo,cpf,rg,rua,ncasa,bairro,cidade,complemento,uf,email,telefone,"
                    + "celular,cep,info) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
            // Entrada de valores
            preparedStatement.setString(1, responsavel.getNome());
            preparedStatement.setString(2, String.valueOf(responsavel.getSexo()));
            preparedStatement.setString(3, responsavel.getCpf().toString());
            preparedStatement.setLong(4, responsavel.getRg());
            preparedStatement.setString(5, responsavel.getEndereco().getRua());
            preparedStatement.setInt(6, responsavel.getEndereco().getNum());
            preparedStatement.setString(7, responsavel.getEndereco().getBairro());
            preparedStatement.setString(8, responsavel.getEndereco().getCidade());
            preparedStatement.setString(9, responsavel.getEndereco().getComplemento());
            preparedStatement.setString(10, responsavel.getEndereco().getUf());
            preparedStatement.setString(11, responsavel.getEmail());
            preparedStatement.setString(12, responsavel.getTelefone());
            preparedStatement.setString(13, responsavel.getCelular());
            preparedStatement.setString(14, responsavel.getEndereco().getCep());
            preparedStatement.setString(15, responsavel.getInformacoes());
            //Execução de comando
            int cod = preparedStatement.executeUpdate();

            System.out.println("ID de cliente " + Integer.toString(cod));
            return true;

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("Erro no cadastro de Cliente");
            return false;
        }   
    }

    
    public static boolean alterar(Responsavel responsavel) {
        try {
            // Preparação de commando SQL para atualização de registros
           preparedStatement = connection.prepareStatement("UPDATE resposavel SET "
                    + "nome=?,sexo=?,cpf=?,rg=?,rua=?,ncasa=?,bairro=?,cidade=?"
                    + ",complemento=?,uf=?,email=?,telefone=?,celular=?,cep=?,info=? "
                    + "WHERE codigo=?");
            // introdução das variaveis no comando
            preparedStatement.setString(1, responsavel.getNome());
            preparedStatement.setString(2, String.valueOf(responsavel.getSexo()));
            preparedStatement.setString(3, responsavel.getCpf().toString());
            preparedStatement.setLong(4, responsavel.getRg());
            preparedStatement.setString(5, responsavel.getEndereco().getRua());
            preparedStatement.setInt(6, responsavel.getEndereco().getNum());
            preparedStatement.setString(7, responsavel.getEndereco().getBairro());
            preparedStatement.setString(8, responsavel.getEndereco().getCidade());
            preparedStatement.setString(9, responsavel.getEndereco().getComplemento());
            preparedStatement.setString(10, responsavel.getEndereco().getUf());
            preparedStatement.setString(11, responsavel.getEmail());
            preparedStatement.setString(12, responsavel.getTelefone());
            preparedStatement.setString(13, responsavel.getCelular());
            preparedStatement.setString(14, responsavel.getEndereco().getCep());
            preparedStatement.setString(15, responsavel.getInformacoes());
            // Codigo do cliente que ira ser alterado
            preparedStatement.setLong(16, responsavel.getCodigo());
            //Executa alteraçõe no banco de dados
            preparedStatement.executeUpdate();

            return true;

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    protected boolean excluir() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected Object[] consultar() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public static boolean existeCPF(CPF cpf) {
        
        try {
            preparedStatement = connection.prepareStatement("SELECT cpf FROM "
                    + "cliente WHERE cpf=?");
            preparedStatement.setString(1, cpf.toString());
            resultset = preparedStatement.executeQuery();

            if (resultset.next()) {
                return true;
            } else {
                return false;
            }

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
